﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ESOL.Common.Entity;
using ESOL.Common;
using ESOL.Business.Mst;
using ESOL.Business.Inv;

namespace ESOL.WinUI.Inv
{
    public partial class frmManageWareHouse : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private MstWarehouseEntity mSelectedWareHouse;
        #endregion

        #region Constructor
        public frmManageWareHouse()
        {
            InitializeComponent();
        }
        #endregion

        #region Function
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByEnum(cmbIsDelete, typeof(DeleteFlag), typeof(string), true);
            UICommon.UICommonFunctions.BindComboboxByEnum(cmbIsLocked, typeof(LockedFlag), typeof(int), true);

            UICommon.UICommonFunctions.BindGridComboboxByEnum(repositoryItemGridLookUpEdit1, typeof(DeleteFlag), typeof(int), true);
            UICommon.UICommonFunctions.BindGridComboboxByEnum(repositoryItemGridLookUpEdit2, typeof(LockedFlag), typeof(int), true);
        }
        private string GetCondition()
        {
            string condition = "";
            if (string.IsNullOrEmpty(txtInvCode.Text) == false)
            {
                condition += " AND WAREHOUSE_CODE LIKE '%" + txtInvCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtInvName.Text) == false)
            {
                condition += " AND WAREHOUSE_NAME LIKE '%" + txtInvName.Text + "%'";
            }
            if (string.IsNullOrEmpty(cmbIsDelete.Text) == false && cmbIsDelete.SelectedValue != null)
            {
                condition += " AND DELETE_FLAG = " + cmbIsDelete.SelectedValue.ToString() + "";
            }
            if (string.IsNullOrEmpty(cmbIsLocked.Text) == false && cmbIsLocked.SelectedValue != null)
            {
                condition += " AND LOCKED = " + cmbIsLocked.SelectedValue.ToString() + "";
            }
            return condition;
        }
        private void ResetButtonEnable()
        {
            btnModify.Enabled = true;
            btnDelete.Enabled = true;
            btnLocationAdd.Enabled = true;
            if (mSelectedWareHouse.DeleteFlag == 1)
            {
                btnModify.Enabled = false;
                btnDelete.Enabled = false;
                btnLocationAdd.Enabled = false;
            }
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "A.WAREHOUSE_CODE";
            DataTable dt = MstWarehouseBs.GetByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        private void BindDetail(string sInvCode)
        {
            if (sInvCode.Equals(""))
            {
                return;
            }
            DataTable dt = MstLocationBs.GetLocationByWarehouseCode(sInvCode);
            if (dt != null)
            {
                gvDetail.DataSource = dt;
            }
        }
        #endregion

        private void frmManageWareHouse_Load(object sender, EventArgs e)
        {
            BindComboBox();
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedWareHouse = new MstWarehouseEntity(dr);
            BindDetail(mSelectedWareHouse.WarehouseCode.ToString());
            ResetButtonEnable();
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_View()
        {
            if (mSelectedWareHouse == null)
            {
                MessageBox.Show("请选择一个仓库！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainWareHouse frm = new frmMaintainWareHouse();
            frm.ThisActionType = ActionType.View;
            frm.SelectedWarehouse = mSelectedWareHouse;
            frm.ShowDialog();
        }

        protected override void Click_Delete()
        {
            if (mSelectedWareHouse == null)
            {
                MessageBox.Show("请选择一个仓库！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (mSelectedWareHouse.DeleteFlag == 1)
            {
                MessageBox.Show("该仓库已被删除！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DataTable dt = InvStorageBs.GetInvStorageByWareHouseCode(mSelectedWareHouse.WarehouseCode);
            if (dt != null)
            {
                MessageBox.Show("该仓库有库存，无法删除！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            DialogResult dr = MessageBox.Show("是否删除选中仓库？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedWareHouse.DeleteFlag = 1;
                bool successful = MstWarehouseBs.UpdateMstWarehouse(mSelectedWareHouse);
                //bool successful = MstWarehouseBs.SetDeleteFlag(mSelectedWareHouse);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedWareHouse == null)
            {
                MessageBox.Show("请选择一个仓库！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainWareHouse frm = new frmMaintainWareHouse();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedWarehouse = mSelectedWareHouse;
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_New()
        {
            frmMaintainWareHouse frm = new frmMaintainWareHouse();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        private void btnLocationAdd_Click(object sender, EventArgs e)
        {
            frmMaintainLocationAdd frm = new frmMaintainLocationAdd();
            if (mSelectedWareHouse == null)
            {
                MessageBox.Show("请选择一个仓库！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frm.SInvCode = mSelectedWareHouse.WarehouseCode;
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindDataGrid();
            }
        }
    }
}